<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="出入用户" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入通过用户"
          clearable
          size="mini"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门禁名称" prop="guardName">
        <el-input
          v-model="queryParams.guardName"
          placeholder="请输入门禁名称"
          clearable
          size="mini"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门禁位置" prop="guardPosition">
        <el-input
          v-model="queryParams.guardPosition"
          placeholder="请输入门禁位置"
          clearable
          size="mini"
          maxlength="30"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出入类型" prop="guardType">
        <el-select v-model="queryParams.guardType" placeholder="请选择出入类型" clearable
                   size="mini">
          <el-option
            v-for="dict in typeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出入结果" prop="guardResult">
        <el-select v-model="queryParams.guardResult" placeholder="请选择出入结果" clearable
                   size="small">
          <el-option
            v-for="dict in resultOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <!--<el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['server:guard:add']"
        >新增
        </el-button>-->
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:guard:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:guard:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="guardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" type="index"  width="80" align="center" prop="guardId"/>
      <el-table-column label="出入用户名" align="center" prop="userName">
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            value="userName"-->
<!--            @click="handleUserName(scope.row)"-->
<!--            v-hasPermi="['server:user:query']"-->
<!--          >{{scope.row.userName}}-->
<!--          </el-button>-->
<!--        </template>-->
      </el-table-column>
      <el-table-column label="通过时间" align="center" prop="createTime"/>
      <el-table-column label="门禁名称" align="center" prop="guardName"/>
      <el-table-column label="门禁位置" align="center" prop="guardPosition"/>
      <el-table-column label="出入类型" align="center" prop="guardType" :formatter="guardTypeFormat"/>
      <el-table-column label="出入结果" align="center" prop="guardResult" :formatter="guardResultFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['server:guard:edit']"
          >修改
          </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['server:guard:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改园区门禁信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出入用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入通过用户"/>
        </el-form-item>
        <el-form-item label="通过时间" prop="userName">
          <el-input v-model="form.createTime" placeholder="请输入通过时间"/>
        </el-form-item>
        <el-form-item label="门禁名称" prop="guardName">
          <el-input v-model="form.guardName" placeholder="请输入门禁名称" maxlength="30"/>
        </el-form-item>
        <el-form-item label="门禁位置" prop="guardPosition">
          <el-input v-model="form.guardPosition" placeholder="请输入门禁位置" maxlength="60"/>
        </el-form-item>
        <el-form-item label="出入类型" prop="guardType">
          <el-select v-model="form.guardType" placeholder="请选择出入类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出入结果" prop="guardResult">
          <el-select v-model="form.guardResult" placeholder="请选择出入类型">
            <el-option
              v-for="dict in resultOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="200"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 通过园区的用户信息框
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出入用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入通过用户"/>
        </el-form-item>
        <el-form-item label="认证时间" prop="anthTime">
          <el-input v-model="form.guardName" placeholder="请输入门禁名称"/>
        </el-form-item>
        <el-form-item label="门禁位置" prop="guardPosition">
          <el-input v-model="form.guardPosition" placeholder="请输入门禁位置"/>
        </el-form-item>
        <el-form-item label="出入类型" prop="guardType">
          <el-select v-model="form.guardType" placeholder="请选择出入类型">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出入结果" prop="guardResult">
          <el-select v-model="form.guardResult" placeholder="请选择出入类型">
            <el-option
              v-for="dict in resultOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->

  </div>
</template>

<script>
  import {
    listGuard,
    getGuard,
    getFace,
    delGuard,
    addGuard,
    updateGuard,
    exportGuard
  } from '@/api/server/garden/guard'

  export default {
    name: 'Guard',
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 园区门禁信息表格数据
        guardList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        typeOptions:[],
        resultOptions:[],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          userName: null,
          guardName: null,
          guardPosition: null,
          guardType: null,
          guardResult: null
        },
        // 表单参数
        form: {}
        ,
        queryForm: {
          userName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
          guardName:[{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
          guardPosition:[{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        },
        // 表单校验
        rules: {}
      }

    },
    created() {
      this.getList()
      this.getDicts('t_guard_type').then(response => {
        this.typeOptions = response.data;
      })
      this.getDicts('t_guard_result').then(response => {
        this.resultOptions = response.data;
      })
    },
    methods: {
      /** 查询园区门禁信息列表 */
      getList() {
        this.loading = true
        listGuard(this.queryParams).then(response => {
          this.guardList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      // 状态字典翻译
      guardTypeFormat(row, column) {
        return this.selectDictLabel(this.typeOptions, row.guardType)
      },
      guardResultFormat(row, column) {
        return this.selectDictLabel(this.resultOptions, row.guardResult)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          guardId: null,
          userName: null,
          guardName: null,
          guardPosition: null,
          guardType: null,
          guardResult: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }

        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.$refs['queryForm'].validate(valid => {
          if (valid) {
            this.getList()
          }
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.guardId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加园区门禁信息'
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const guardId = row.guardId || this.ids
        getGuard(guardId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改园区门禁信息'
        })

      },
      // handleUserName(row) {
      //   this.reset()
      //   const userId = ''
      //   console.log(row)
      //   getFace(userId).then(response => {
      //     this.form = response.data
      //     this.open = true
      //     this.title = '通过用户人脸数据'
      //   })
      // },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
            if (valid) {
              if (this.form.guardId != null) {
                updateGuard(this.form).then(response => {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                })

              } else {
                addGuard(this.form).then(response => {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                })

              }
            }
          }
        )

      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const guardIds = row.guardId || this.ids
          this.$confirm('是否确认删除该数据项?', '警告', {
            cancelButtonClass: 'btn-custom-cancel',
            closeOnClickModal: false,
              type: 'warning'
            }
          ).then(function() {
            return delGuard(guardIds)
          }).then(() => {
            this.getList()
            this.msgSuccess('删除成功')
          })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$confirm('是否确认导出所有园区门禁信息数据项?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type:"info"
        }).then(function() {
          return exportGuard(queryParams)
        }).then(response => {
          this.download(response.msg)
        })
      }
    }
  }
</script>
